package com.service;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.dto.UserAuthDTO;

public class UserAuthService 
{
	public void signUp(UserAuthDTO dto)
	{
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(dto);
		transaction.commit();
		session.close();
		factory.close();
		
	}
	
	public UserAuthDTO signIn(UserAuthDTO dto)
	{
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Query query = session.createQuery("from UserAuthDTO where userName=? and password=?");
		query.setString(0, dto.getUserName());
		query.setString(1, dto.getPassword());
		
		List list = query.list();
		
		if(list.size()==1)
		{
			System.out.println("Account Verified, Welcome...");
			dto = (UserAuthDTO)list.get(0); 
		}
		else
		{
			System.out.println("UserName and Password does not match");
			dto = null;
		}
		return dto;
	}
}
